import TypeJudg from '@/utils/typeJudgment';


const rules = {
  eq: {
    text: '等于',
    status: 'eq',
    compared: (curVal: any, ruleVal: any) => {
      return typeJudgFn(ruleVal,{
        isObject:()=>{
          return curVal === ruleVal.value;
        },
        isArray:()=>{
          return curVal === ruleVal;
        },
        isString:()=>{
          return JSON.stringify(curVal) === JSON.stringify(ruleVal);
        }
      })
    }
  },
  ne: {
    text: '不等于', status: 'ne', 
    compared: (curVal: any, ruleVal: any) => {
      return typeJudgFn(ruleVal,{
        isObject:()=>{
          return curVal !== ruleVal.value;
        },
        isArray:()=>{
          return curVal !== ruleVal;
        },
        isString:()=>{
          return JSON.stringify(curVal) !== JSON.stringify(ruleVal);
        }
      })
    }
  },
  in: { 
    text: '等于任意一个', 
    status: 'in' ,
    compared: (curVal: any, ruleVal: any) => {

      return typeJudgFn(ruleVal,{
        isArray:()=>{
          return ruleVal.some((v:any) =>v === curVal);
        }
      })
      
    }
  },
  nin: { 
    text: '不等于任意一个', 
    status: 'nin' ,
    compared: (curVal: any, ruleVal: any) => {
      return typeJudgFn(ruleVal,{
        isArray:()=>{
          return ruleVal.every((v:any) =>v !== curVal);
        }
      })
    }
  },
  like: { 
    text: '包含', 
    status: 'like' ,
    compared: (curVal: any, ruleVal: any) => {
      return typeJudgFn(ruleVal,{
        isArray:()=>{
          return ruleVal.some((v:any) =>v === curVal);
        }
      })
    }
  },
  unlike: { 
    text: '不包含', 
    status: 'unlike' ,
    compared: (curVal: any, ruleVal: any) => {
      return typeJudgFn(ruleVal,{
        isArray:()=>{
          return ruleVal.every((v:any) =>v !== curVal);
        }
      })
    }
  },
  empty: { 
    text: '为空', 
    status: 'empty' ,
    compared: (curVal: any, ruleVal: any) => {
      return !curVal ? true : false
    }
  },
  not_empty: { 
    text: '不为空', 
    status: 'not_empty' ,
    compared: (curVal: any, ruleVal: any) => {
      return curVal ? true : false
    }
  },
  gte: { 
    text: '大于等于', 
    status: 'gte' ,
    compared: (curVal: any, ruleVal: any) => {
      return parseFloat(curVal) >= parseFloat(ruleVal)
    }
  },
  lte: { 
    text: '小于等于', 
    status: 'lte' ,
    compared: (curVal: any, ruleVal: any) => {
      return parseFloat(curVal) <= parseFloat(ruleVal)
    }
  },
}

export default rules;

function typeJudgFn(val:any,{isObject,isString,isArray}:any){
  if(TypeJudg.isObject(val)){
    return isObject()
  }
  else if(TypeJudg.isString(val)){
    return isString()
  }
  else if(TypeJudg.isArray(val)){
    return isArray()
  }
  return false;
}